// ==UserScript==
// @name         learn
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  懒人听课，让你腾出更多时间写代码。
// @author       You
// @match        http://training.sac.net.cn/learnspace/learn/learn/blue/courseware_index.action*
// @grant        none
// ==/UserScript==

(function() {
  'use strict';

  var ids = [];
  var current = "";
  var current_idx = 0;
  var timer = null;

    var vconlists = document.getElementsByClassName("vconlist");

    for (var _v = 0; _v < vconlists.length; _v++) {
        var lis = vconlists[_v].getElementsByTagName('li');
        for (var lid = 0; lid < lis.length; lid++) {
            var id = lis[lid].getAttribute('id');
            ids.push(id);
        }
    }

    currentState();


    function currentState() {
        for (var ids_i = 0; ids_i < ids.length; ids_i++) {
            var li = document.getElementById(ids[ids_i]);

            if (li.classList.contains("select")) {
                current = ids[ids_i];
                current_idx = ids_i;
            }
        }


        //console.log(ids);
        //console.log('current:' + current);
        //console.log('current_idx:' + current_idx)
    }

  timer = setInterval(function () {
    // var test = $(window.frames["mainFrame"].document).find('#container_display_button_play')
    // console.log(test[0])
      currentState();
    var replay = $(window.frames["mainFrame"].document).find('#container_display_button_replay');
    // console.log(replay[0])
    if (!replay[0]) {
        console.log('还没播放完')
        return false;
    } else {
        console.log('播放完')
        // clearInterval(timer);
        var next_idx = current_idx + 1;
        if (next_idx !== ids.length) {
            var cur = document.getElementById(ids[current_idx]);
            var next = document.getElementById(ids[next_idx]);
            current_idx = next_idx;
            next.click();

            next.getElementsByTagName('a')[0].click();
            cur.classList.remove("select");
            next.classList.add("select");
        } else {
          alert('请做测试题!');
          clearInterval(timer);
        }
      }
  }, 3000);

})();